package com.luoran.car.controller;


import cn.hutool.json.JSONUtil;
import cn.hutool.jwt.JWT;
import com.luoran.car.domain.CarRentals;
import com.luoran.car.service.CarRentalsService;
import com.luoran.car.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.EncodeException;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/carren")
public class CarRentalsController {
    @Autowired
    CarRentalsService carRentalsService;

    @Autowired
    HttpServletRequest request;






    @RequestMapping("/list")
    public Result getlist()
    {
        String token = request.getHeader("token");
        JWT of = JWT.of(token);
        System.out.println("获取的id为"+token);
        Integer id = (Integer) of.getPayload("id");
        System.out.println("获取的id为"+id);

        List<CarRentals> list = carRentalsService.selectlist(id);

        return Result.success(list);

    }


    @RequestMapping("/getquxiao")
    public  Result getdel(Integer id)
    {
        carRentalsService.updateByCar(id);
        return Result.success();
    }



    @Scheduled(cron = "0/6 * * * * ?")
    public void fasong() throws IOException, EncodeException {
        List<Map<String, Object>> list =carRentalsService.getbaobiao();
        String s = JSONUtil.toJsonStr(list);
        ManagementWebsocket.sendMessage(s);
    }








}

